package com.crm.repository;

import com.crm.pojo.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {
    /**
     * 根据姓名查询用户
     * @param usrName 姓名
     * @return 返回一个用户
     * @throws Exception
     */
    public User findUserByUsrName(String usrName);

    /**
     * 查询角色为销售主管的用户
     * @return 返回一个集合
     * @throws Exception
     */
    @Query(value = "select u from User u where u.role.roleId = (select r.roleId from Role r where r.roleName = '客户经理')")
    public List<User> findUsersByRole() throws Exception;

    /**
     * 根据用户编号查询该用户是否为客户经理
     * @param usrId 用户编号
     * @return 返回一个用户
     * @throws Exception
     */
    @Query(value = "select u from User u where u.role.roleId = " +
            "(select r.roleId from Role r where r.roleName = '客户经理') and u.usrId = ?1")
    public User findUserByRoleNameAndUsrId(Long usrId) throws Exception;
}